﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using JCTimes.DomainModel.Repository;
using JCTimes.DomainModel.Views;
using JCTimes.Portal.Infrastructure;
using JCTimes.Portal.Models;
using JCTimes.Portal.Specification;
using System.Data;

namespace JCTimes.Portal.Controllers
{
    [Permission]
    public class UsersLogController : Controller
    {
        private IUserRepository dmRep = null;
        private const int pageSize = 10;

        public UsersLogController(IUserRepository repository)
        {
            dmRep = repository;
        }
    

        public ActionResult Index()
        {
            return View();
        }


        public PartialViewResult Grid(int? pageIndex, Searcher searcher)
        {

            var pred = UsersLogSpecification.Get_V_Lst_UsersLog_Pred(searcher);
            var model = new GridView<V_UsersLog>
            {
                Searcher = searcher,
                PagedList = dmRep.GetList<V_UsersLog>().Where(pred).OrderByDescending(u => u.LoginDate).ToPagedList(pageIndex ?? 1, pageSize)
            };
            return PartialView("_gridview", model);
        }

        [ActionName("Export")]
        [HttpPost]
        public JsonResult PreExportSellThrough(Searcher searcher)
        {
            var pred = UsersLogSpecification.Get_V_Lst_UsersLog_Pred(searcher);

            var list = dmRep.GetList<V_UsersLog>();

            bool hasdata = list.Where(pred).Count() > 0;
            return Json(new { searcher = searcher.ToString(), hasdata = hasdata });
        }


        /// <summary>
        /// export for users log
        /// </summary>
        /// <param name="searcher"></param>
        public void Export(Searcher searcher)
        {
            var pred = UsersLogSpecification.Get_V_Lst_UsersLog_Pred(searcher);
            var list = dmRep.GetList<V_UsersLog>();
            var lst = list.Where(pred).OrderByDescending(d => d.LoginDate).ToList();

            string[] filters = new string[] {
                "UserName","Name","RoleName", "LoginDate"
               
            };

            var dt = lst.ToDataTable(filters);
            string[] columnname = new string[] { "用户名", "姓名", "角色", "登录时间" };

            ExcelHelper.ExportToExcel(null, "UsersLog", dt, columnname, filters, false);
        }

    }
}
